package com.my.study.util;

import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.concurrent.*;

public class ThreadPoolUtil {

    private static final int CORE_POOL_SIZE = 10;

    private static final int MAX_POOL_SIZE = 20;

    private static final long KEEP_ALIVE_TIME = 1000;

    private static final int CAPACITY = 20;

    private static ThreadPoolExecutor executor;

    static {
        // 创建简单线程池 ExecutorService executorService = Executors.newSingleThreadExecutor();
        executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(CAPACITY), new DefaultThreadFactory("testPool"));
    }

    public static void submit(Runnable runnable) {
        executor.submit(runnable);
    }

    public static void execute(Runnable runnable) {
        executor.execute(runnable);
    }

    public static <T> T submit(Callable<T> callable) throws ExecutionException, InterruptedException {
        return executor.submit(callable).get();
    }

}
